| Conditions | 8 |
| Total Lines | 92 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import React, { Component } from 'react'; |
||
| 29 | |||
| 30 | componentDidMount() { |
||
| 31 | console.log(this.props); |
||
| 32 | this._isMounted = true; |
||
| 33 | // fetch realtime data at the first time |
||
| 34 | let isResponseOK = false; |
||
| 35 | if (this.props.distributionSystemID != undefined) { |
||
| 36 | fetch(APIBaseURL + '/reports/distributionsystem?distributionsystemid=' + this.props.distributionSystemID, { |
||
| 37 | method: 'GET', |
||
| 38 | headers: { |
||
| 39 | "Content-type": "application/json", |
||
| 40 | "User-UUID": getCookieValue('user_uuid'), |
||
| 41 | "Token": getCookieValue('token') |
||
| 42 | }, |
||
| 43 | body: null, |
||
| 44 | |||
| 45 | }).then(response => { |
||
| 46 | if (response.ok) { |
||
| 47 | isResponseOK = true; |
||
| 48 | } |
||
| 49 | return response.json(); |
||
| 50 | }).then(json => { |
||
| 51 | if (isResponseOK) { |
||
| 52 | console.log(json); |
||
| 53 | let pointList = []; |
||
| 54 | json.forEach((currentCircuit, circuitIndex) => { |
||
| 55 | json[circuitIndex]['points'].forEach((currentPoint, pointIndex) => { |
||
| 56 | let pointItem = {} |
||
| 57 | pointItem['circuit'] = currentCircuit['name']; |
||
| 58 | pointItem['point'] = currentPoint['name']; |
||
| 59 | pointItem['value'] = currentPoint['value']; |
||
| 60 | pointItem['units'] = currentPoint['units']; |
||
| 61 | pointList.push(pointItem); |
||
| 62 | }); |
||
| 63 | }); |
||
| 64 | if (this._isMounted) { |
||
| 65 | this.setState({ |
||
| 66 | pointList: pointList, |
||
| 67 | }); |
||
| 68 | } |
||
| 69 | } else { |
||
| 70 | toast.error(json.description) |
||
| 71 | } |
||
| 72 | }).catch(err => { |
||
| 73 | console.log(err); |
||
| 74 | }); |
||
| 75 | }; |
||
| 76 | |||
| 77 | //fetch realtime data at regular intervals |
||
| 78 | this.refreshInterval = setInterval(() => { |
||
| 79 | let isResponseOK = false; |
||
| 80 | fetch(APIBaseURL + '/reports/distributionsystem?distributionsystemid=' + this.props.distributionSystemID, { |
||
| 81 | method: 'GET', |
||
| 82 | headers: { |
||
| 83 | "Content-type": "application/json", |
||
| 84 | "User-UUID": getCookieValue('user_uuid'), |
||
| 85 | "Token": getCookieValue('token') |
||
| 86 | }, |
||
| 87 | body: null, |
||
| 88 | |||
| 89 | }).then(response => { |
||
| 90 | if (response.ok) { |
||
| 91 | isResponseOK = true; |
||
| 92 | } |
||
| 93 | return response.json(); |
||
| 94 | }).then(json => { |
||
| 95 | if (isResponseOK) { |
||
| 96 | console.log(json); |
||
| 97 | let pointList = []; |
||
| 98 | json.forEach((currentCircuit, circuitIndex) => { |
||
| 99 | json[circuitIndex]['points'].forEach((currentPoint, pointIndex) => { |
||
| 100 | let pointItem = {} |
||
| 101 | pointItem['circuit'] = currentCircuit['name']; |
||
| 102 | pointItem['point'] = currentPoint['name']; |
||
| 103 | pointItem['value'] = currentPoint['value']; |
||
| 104 | pointItem['units'] = currentPoint['units']; |
||
| 105 | pointList.push(pointItem); |
||
| 106 | }); |
||
| 107 | }); |
||
| 108 | |||
| 109 | if (this._isMounted) { |
||
| 110 | this.setState({ |
||
| 111 | pointList: pointList, |
||
| 112 | }); |
||
| 113 | } |
||
| 114 | } else { |
||
| 115 | toast.error(json.description) |
||
| 116 | } |
||
| 117 | }).catch(err => { |
||
| 118 | console.log(err); |
||
| 119 | }); |
||
| 120 | }, (60 + Math.floor(Math.random() * Math.floor(10))) * 1000); // use random interval to avoid paralels requests |
||
| 121 | } |
||
| 163 |